package com.wingice.system.controller;

import com.wingice.common.web.ErrorCodeInfo;
import com.wingice.system.entity.SysDepartment;
import com.wingice.system.exception.ResultBean;
import com.wingice.system.service.SysDepartmentService;
import com.wingice.system.vo.SysDepartmentView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 部门信息 前端控制器
 * </p>
 *
 * @author 胡昊
 * @since 2023-04-10
 */
@RestController
@RequestMapping("/sysDepartment")
@Tag(name = "部门信息")
public class SysDepartmentController {

    @Resource
    private SysDepartmentService sysDepartmentService;

    @PreAuthorize("@knifeSecurity.hasPermission('sysDepartment:insert')")
    @PostMapping("/insert")
    @Operation(summary = "新增部门")
    public ResultBean<Boolean> insert(@RequestBody SysDepartment sysDepartment) {
        return ResultBean.restResult(sysDepartmentService.insertSysDepartment(sysDepartment), ErrorCodeInfo.OK);
    }

    @PreAuthorize("@knifeSecurity.hasPermission('sysDepartment:deleteBatch')")
    @PostMapping("/deleteBatch")
    @Operation(summary = "删除部门")
    public ResultBean<Boolean> deleteBatch(@RequestBody List<String> idList) {
        return ResultBean.restResult(sysDepartmentService.deleteBatch(idList), ErrorCodeInfo.OK);
    }

    @PostMapping("update")
    @PreAuthorize("@knifeSecurity.hasPermission('sysDepartment:update')")
    @Operation(summary = "更新部门")
    public ResultBean<Boolean> update(@RequestBody SysDepartment sysDepartment) {
        return ResultBean.restResult(sysDepartmentService.updateSysDepartment(sysDepartment), ErrorCodeInfo.OK);
    }


    @PreAuthorize("isAuthenticated()")
    @GetMapping("/treeSysDepartment")
    @Operation(summary = "查询部门树")
    public ResultBean<List<SysDepartmentView>> treeSysDepartment() {
        return ResultBean.restResult(sysDepartmentService.treeSysDepartment(), ErrorCodeInfo.OK);
    }
}
